IF(NOT WIN32)
  IF(APPLE)
    FIND_LIBRARY(CARBON NAMES Carbon)
    FIND_LIBRARY(COCOA NAMES Cocoa)
    FIND_PACKAGE(OpenGL REQUIRED)
  ELSE()
    SET(OLD_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
    SET(CMAKE_FIND_LIBRARY_SUFFIXES .so)
    FIND_PACKAGE(X11)
    FIND_PACKAGE(XF86VidMode)
    FIND_PACKAGE(OpenGL REQUIRED)
    SET(CMAKE_FIND_LIBRARY_SUFFIXES ${OLD_CMAKE_FIND_LIBRARY_SUFFIXES})
  ENDIF()
ELSE()
  FIND_PACKAGE(OpenGL REQUIRED)
ENDIF()

FILE(GLOB SRC *.cpp *.h *.def *.rc)

IF(APPLE)
  FILE(GLOB MAC_SRC mac/*.h mac/*.m mac/*.mm mac/*.cpp)
  SET(SRC ${SRC} ${MAC_SRC})
  SET_SOURCE_FILES_PROPERTIES(${SRC}
    PROPERTIES COMPILE_FLAGS "-x objective-c++")
ENDIF()

IF(WIN32)
  SET(NLDRV_OGL_LIB "nel_drv_opengl_win")
ELSE()
  SET(NLDRV_OGL_LIB "nel_drv_opengl")
ENDIF()

# This helps to debug issue 310
IF(DEBUG_OGL_SPECULAR_FALLBACK)
  ADD_DEFINITIONS(-DDEBUG_OGL_SPECULAR_FALLBACK)
ENDIF()
IF(DEBUG_OGL_COMBINE43_DISABLE)
  ADD_DEFINITIONS(-DDEBUG_OGL_COMBINE43_DISABLE)
ENDIF()

NL_TARGET_DRIVER(${NLDRV_OGL_LIB} ${SRC})

INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR})

TARGET_LINK_LIBRARIES(${NLDRV_OGL_LIB} nel3d nelmisc ${OPENGL_gl_LIBRARY})
NL_DEFAULT_PROPS(${NLDRV_OGL_LIB} "NeL, Driver, Video: OpenGL")
NL_ADD_LIB_SUFFIX(${NLDRV_OGL_LIB})
NL_ADD_RUNTIME_FLAGS(${NLDRV_OGL_LIB})

IF(WIN32)
  ADD_DEFINITIONS(-DDRIVER_OPENGL_EXPORTS)
ENDIF()

IF(APPLE)
  TARGET_LINK_LIBRARIES(${NLDRV_OGL_LIB} ${CARBON})
  TARGET_LINK_LIBRARIES(${NLDRV_OGL_LIB} ${COCOA})
ENDIF()

IF(UNIX AND NOT APPLE)
  TARGET_LINK_LIBRARIES(${NLDRV_OGL_LIB} ${X11_X11_LIB})
  IF(XF86VidMode_FOUND)
    INCLUDE_DIRECTORIES(${XF86VidMode_INCLUDE_DIR})
    ADD_DEFINITIONS(${XF86VidMode_DEFINITIONS})
    TARGET_LINK_LIBRARIES(${NLDRV_OGL_LIB} ${XF86VidMode_LIBRARY})
  ENDIF()
  IF(X11_Xrandr_FOUND)
    INCLUDE_DIRECTORIES(${X11_Xrandr_INCLUDE_PATH})
    ADD_DEFINITIONS(-DHAVE_XRANDR)
    TARGET_LINK_LIBRARIES(${NLDRV_OGL_LIB} ${X11_Xrandr_LIB})
  ENDIF()
  IF(X11_Xrender_FOUND)
    INCLUDE_DIRECTORIES(${X11_Xrender_INCLUDE_PATH})
    ADD_DEFINITIONS(-DHAVE_XRENDER)
    TARGET_LINK_LIBRARIES(${NLDRV_OGL_LIB} ${X11_Xrender_LIB})
  ENDIF()
  IF(X11_Xcursor_FOUND)
    INCLUDE_DIRECTORIES(${X11_Xcursor_INCLUDE_PATH})
    ADD_DEFINITIONS(-DHAVE_XCURSOR)
    TARGET_LINK_LIBRARIES(${NLDRV_OGL_LIB} ${X11_Xcursor_LIB})
  ENDIF()
  IF(X11_Xext_FOUND)
    TARGET_LINK_LIBRARIES(${NLDRV_OGL_LIB} ${X11_Xext_LIB})
  ENDIF()
  # libraries needed to be linked while linking to static X11 libraries
#  FIND_LIBRARY(XCB_LIBRARY
#    NAMES xcb
#    HINTS ${X11_LIB_SEARCH_PATH})
#  IF(XCB_LIBRARY)
#    TARGET_LINK_LIBRARIES(${NLDRV_OGL_LIB} ${XCB_LIBRARY})
#  ENDIF()
ENDIF()

IF(WITH_PCH)
  ADD_NATIVE_PRECOMPILED_HEADER(${NLDRV_OGL_LIB} ${CMAKE_CURRENT_SOURCE_DIR}/stdopengl.h ${CMAKE_CURRENT_SOURCE_DIR}/stdopengl.cpp)
ENDIF()

IF((WITH_INSTALL_LIBRARIES AND WITH_STATIC_DRIVERS) OR NOT WITH_STATIC_DRIVERS)
  INSTALL(TARGETS ${NLDRV_OGL_LIB} LIBRARY DESTINATION ${NL_DRIVER_PREFIX} ARCHIVE DESTINATION ${NL_LIB_PREFIX} RUNTIME DESTINATION ${NL_DRIVER_PREFIX} COMPONENT drivers3d)
  IF(WITH_MAXPLUGIN)
    INSTALL(TARGETS ${NLDRV_OGL_LIB} RUNTIME DESTINATION maxplugin COMPONENT drivers3d)
  ENDIF()
ENDIF()
